﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;


class FindFilesWithString
{

    public const string FOR_SEARCH = "portokal";

    static void FindFile(DirectoryInfo di)
    {
        foreach (var file in di.GetFiles())
        {
		     if(file.Name.EndsWith(".txt"))
             {
                SearchForString(file.FullName, FOR_SEARCH);
                //Console.WriteLine(file.FullName);
             }
    	}

        foreach (var directory in di.GetDirectories())
	    {
		   FindFile(directory);
	    }
    }

    static void SearchForString(string fullpath, string pattern)
    {

        using (var reader = new StreamReader(fullpath))
        {
            using (var writer = new StreamWriter("result.txt", true))
            {
                string line = "";
                while ((line = reader.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                    if(line.Contains(pattern))
                    {
                      //  Console.WriteLine("ALOOOO");
                        writer.WriteLine(line);
                        break;
                    }
                }
            }
        }
    }


    static void Main()
    {
        //StreamWriter writer = new StreamWriter("result.txt");
        DirectoryInfo dInfo  = new DirectoryInfo(@"C:\TEST");
        FindFile(dInfo);
    }
}

